<?php
declare (strict_types=1);

namespace app\admin\controller;

use app\common\builder\TableBuilder;
use app\common\builder\TableFront;
use app\common\model\Order;
use app\common\model\OrderProduct;
use app\common\model\ProductCate;
use app\common\model\Product;
use app\common\model\Area;
use app\common\model\StatisticsAreaMonth;
use app\common\model\StatisticsAreaYear;
use app\common\model\StatisticsProductMonth;
use app\common\model\StatisticsProductYear;
use app\common\model\User;
use think\facade\View;
use think\response\Json;

class Statistics extends Base
{
    public function setSearch(): TableFront
    {
        return TableFront::getInstance();
    }



    #---------------------------------------------------------------会员统计start-----------------------------------------#
    /**
     * 会员统计
     */
    public function userYear(): string
    {
        $tableFront = TableFront::getInstance()
            ->addRow(['field' => 'user_count', 'title' => '年度总人数'])
            ->addRow(['field' => 'user_add', 'title' => '年度新增人数'])
            ->addRow(['field' => 'user_rate', 'title' => '年度增长率']);

        return TableBuilder::getInstance()
            ->addColumns($tableFront)
            ->setSearch($this->setSearch(),false)
            ->fetch('user');
    }

    /**
     * 会员统计
     */
    public function userMonth(): string
    {
        $tableFront = TableFront::getInstance()
            ->addRow(['field' => 'user_count', 'title' => '月度总人数'])
            ->addRow(['field' => 'user_add', 'title' => '月度新增人数'])
            ->addRow(['field' => 'user_rate', 'title' => '月度增长率']);

        return TableBuilder::getInstance()
            ->addColumns($tableFront)
            ->setSearch($this->setSearch(),false)
            ->fetch('user');
    }

    public function getUserList(): Json
    {
        $name = $this->request->param('name', '');

        try {
            $lists = $list = [];
            if('Statistics_userYear' == $name){
                $list['user_count'] = User::whereYear('create_time')->count();
                $list['last_user_count'] = User::whereYear('create_time', 'last year')->count();
            }elseif ('Statistics_userMonth' == $name){
                $list['user_count'] = User::whereMonth('create_time')->count();
                $list['last_user_count'] = User::whereMonth('create_time', 'last month')->count();
            }

            $list['user_add'] = $list['user_count'] - $list['last_user_count'];
            $list['user_rate'] = $list['last_user_count'] > 0 ? round(100*$list['user_add']/$list['last_user_count'], 2).'%' : '-';
            $lists[] = $list;
            return json(['code' => 0, 'data' => $lists, 'count' => 1]);

        } catch (\Exception $e) {
            return json(['code'=>-1, 'message'=>$e->getMessage()]);
        }
    }

    #---------------------------------------------------------------会员统计end-------------------------------------------#

    #---------------------------------------------------------------业绩统计start-----------------------------------------#
    /**
     * 业绩统计
     */
    public function priceYear(): string
    {
        $tableFront = TableFront::getInstance()
            ->addRow(['field' => 'price', 'title' => '年度总业绩'])
            ->addRow(['field' => 'price_add', 'title' => '年度新增业绩'])
            ->addRow(['field' => 'price_rate', 'title' => '年度增长率']);

        return TableBuilder::getInstance()
            ->addColumns($tableFront)
            ->setSearch($this->setSearch(),false)
            ->fetch('price');
    }

    /**
     * 业绩统计
     */
    public function priceMonth(): string
    {
        $tableFront = TableFront::getInstance()
            ->addRow(['field' => 'price', 'title' => '月度总业绩'])
            ->addRow(['field' => 'price_add', 'title' => '月度新增业绩'])
            ->addRow(['field' => 'price_rate', 'title' => '月度增长率']);

        return TableBuilder::getInstance()
            ->addColumns($tableFront)
            ->setSearch($this->setSearch(),false)
            ->fetch('price');
    }

    public function getPriceList(): Json
    {
        $name = $this->request->param('name', '');

        try {
            $lists = $list = [];
            if('Statistics_priceYear' == $name){
                $list['price'] = Order::whereYear('pay_time')->sum('real_amount');
                $list['last_price'] = Order::whereYear('pay_time', 'last year')->sum('real_amount');
            }elseif ('Statistics_priceMonth' == $name){
                $list['price'] = Order::whereMonth('pay_time')->sum('real_amount');
                $list['last_price'] = Order::whereMonth('pay_time', 'last month')->sum('real_amount');
            }

            $list['price_add'] = $list['price'] - $list['last_price'];
            $list['price_rate'] = $list['last_price'] > 0 ? round(100*$list['price_add']/$list['last_price'], 2).'%' : '-';
            $lists[] = $list;
            return json(['code' => 0, 'data' => $lists, 'count' => 1]);

        } catch (\Exception $e) {
            return json(['code'=>-1, 'message'=>$e->getMessage()]);
        }
    }

    #---------------------------------------------------------------业绩统计end-------------------------------------------#

    /**
     * 产品总成本
     * @return string
     */
    public function productCost(): string
    {
        $product_costs = Product::where(['is_del'=>0])->sum('cost');
        $freight = Order::where('pay_state',2)->sum('freight');

        View::assign(['product_costs' => $product_costs]);
        View::assign(['freight' => $freight]);
        return View::fetch();
    }

    #---------------------------------------------------------------分区业绩统计start-----------------------------------------#

    /**
     * 年度消费人数
     * @return string
     * @throws \Exception
     */
    public function cateUserYear(): string
    {
        $tableFront = TableFront::getInstance()
            ->addRow(['field' => 'title', 'title' => '专区'])
            ->addRow(['field' => 'user_count', 'title' => '年度消费人数'])
            ->addRow(['field' => 'user_add', 'title' => '年度新增消费人数'])
            ->addRow(['field' => 'user_rate', 'title' => '年度增长率']);

        return TableBuilder::getInstance()
            ->addColumns($tableFront)
            ->setSearch($this->setSearch(),false)
            ->fetch('cate');
    }

    /**
     * 月度消费人数
     * @return string
     * @throws \Exception
     */
    public function cateUserMonth(): string
    {
        $tableFront = TableFront::getInstance()
            ->addRow(['field' => 'title', 'title' => '专区'])
            ->addRow(['field' => 'user_count', 'title' => '月度消费人数'])
            ->addRow(['field' => 'user_add', 'title' => '月度新增消费人数'])
            ->addRow(['field' => 'user_rate', 'title' => '月度增长率']);

        return TableBuilder::getInstance()
            ->addColumns($tableFront)
            ->setSearch($this->setSearch(),false)
            ->fetch('cate');
    }

    /**
     * 年度业绩
     * @return string
     * @throws \Exception
     */
    public function catePriceYear(): string
    {
        $tableFront = TableFront::getInstance()
            ->addRow(['field' => 'title', 'title' => '专区'])
            ->addRow(['field' => 'price', 'title' => '年度业绩'])
            ->addRow(['field' => 'price_add', 'title' => '年度新增业绩'])
            ->addRow(['field' => 'price_rate', 'title' => '年度增长率']);

        return TableBuilder::getInstance()
            ->addColumns($tableFront)
            ->setSearch($this->setSearch(),false)
            ->fetch('cate');
    }

    /**
     * 月度业绩
     * @return string
     * @throws \Exception
     */
    public function catePriceMonth(): string
    {
        $tableFront = TableFront::getInstance()
            ->addRow(['field' => 'title', 'title' => '专区'])
            ->addRow(['field' => 'price', 'title' => '月度业绩'])
            ->addRow(['field' => 'price_add', 'title' => '月度新增业绩'])
            ->addRow(['field' => 'price_rate', 'title' => '月度增长率']);

        return TableBuilder::getInstance()
            ->addColumns($tableFront)
            ->setSearch($this->setSearch(),false)
            ->fetch('cate');
    }

    public function getCateList(): Json
    {
        $param = $this->request->param();
        $name = $param['name'];

        try {
            $list = ProductCate::where('pid', 0)->field('id,title')->select();

            foreach($list as &$item){
                if('Statistics_cateUserYear' == $name){
                    $item['user_count'] = OrderProduct::where(['cate_1'=>$item['id']])->whereYear('create_time')->count();
                    $item['last_user_count'] = OrderProduct::where(['cate_1'=>$item['id']])->whereYear('create_time', 'last year')->count();

                    $item['user_add'] = $item['user_count'] - $item['last_user_count'];
                    $item['user_rate'] = $item['last_user_count'] > 0 ? round(100*$item['user_add']/$item['last_user_count'], 2).'%' : '-';
                }elseif ('Statistics_cateUserMonth' == $name){
                    $item['user_count'] = OrderProduct::where(['cate_1'=>$item['id']])->whereMonth('create_time')->count();
                    $item['last_user_count'] = OrderProduct::where(['cate_1'=>$item['id']])->whereMonth('create_time', 'last year')->count();

                    $item['user_add'] = $item['user_count'] - $item['last_user_count'];
                    $item['user_rate'] = $item['last_user_count'] > 0 ? round(100*$item['user_add']/$item['last_user_count'], 2).'%' : '-';
                }elseif ('Statistics_catePriceYear' == $name){
                    $item['price'] = OrderProduct::where(['cate_1'=>$item['id']])->whereYear('create_time')->sum('vip_price');
                    $item['last_price'] = OrderProduct::where(['cate_1'=>$item['id']])->whereYear('create_time', 'last year')->sum('vip_price');

                    $item['price_add'] = $item['price'] - $item['last_price'];
                    $item['price_rate'] = $item['last_price'] > 0 ? round(100*$item['price_add']/$item['last_price'], 2).'%' : '-';
                }elseif ('Statistics_catePriceMonth' == $name){
                    $item['price'] = OrderProduct::where(['cate_1'=>$item['id']])->whereMonth('create_time')->sum('vip_price');
                    $item['last_price'] = OrderProduct::where(['cate_1'=>$item['id']])->whereMonth('create_time', 'last year')->sum('vip_price');

                    $item['price_add'] = $item['price'] - $item['last_price'];
                    $item['price_rate'] = $item['last_price'] > 0 ? round(100*$item['price_add']/$item['last_price'], 2).'%' : '-';
                }
            }

            return json(['code' => 0, 'data' => $list, 'count' => count($list)]);

        } catch (\Exception $e) {
            return json(['code'=>-1, 'message'=>$e->getMessage()]);
        }
    }

    /*public function getCateList(): Json
    {
        $param = $this->request->param();
        $name = $param['name'];

        try {
            $list = [
                [
                    'id' => 1,
                    'title' => 'VIP专区'
                ],[
                    'id' => 2,
                    'title' => '店主专区'
                ],[
                    'id' => 3,
                    'title' => '盟主专区'
                ],[
                    'id' => 999,
                    'title' => '消费专区'
                ],
            ];

            foreach($list as &$item){
                if('Statistics_cateUserYear' == $name){
                    if(in_array($item['id'],[1,2,3])){
                        $item['user_count'] = OrderProduct::where(['cate_1'=>$item['id']])->whereYear('create_time')->count();
                        $item['last_user_count'] = OrderProduct::where(['cate_1'=>$item['id']])->whereYear('create_time', 'last year')->count();
                    }else{
                        $item['user_count'] = OrderProduct::whereNotIn('cate_1', [1,2,3])->whereYear('create_time')->count();
                        $item['last_user_count'] = OrderProduct::whereNotIn('cate_1', [1,2,3])->whereYear('create_time', 'last year')->count();
                    }

                    $item['user_add'] = $item['user_count'] - $item['last_user_count'];
                    $item['user_rate'] = $item['last_user_count'] > 0 ? round(100*$item['user_add']/$item['last_user_count'], 2).'%' : '-';
                }elseif ('Statistics_cateUserMonth' == $name){
                    if(in_array($item['id'],[1,2,3])){
                        $item['user_count'] = OrderProduct::where(['cate_1'=>$item['id']])->whereMonth('create_time')->count();
                        $item['last_user_count'] = OrderProduct::where(['cate_1'=>$item['id']])->whereMonth('create_time', 'last year')->count();
                    }else{
                        $item['user_count'] = OrderProduct::whereNotIn('cate_1', [1,2,3])->whereMonth('create_time')->count();
                        $item['last_user_count'] = OrderProduct::whereNotIn('cate_1', [1,2,3])->whereMonth('create_time', 'last year')->count();
                    }

                    $item['user_add'] = $item['user_count'] - $item['last_user_count'];
                    $item['user_rate'] = $item['last_user_count'] > 0 ? round(100*$item['user_add']/$item['last_user_count'], 2).'%' : '-';
                }elseif ('Statistics_catePriceYear' == $name){
                    if(in_array($item['id'],[1,2,3])){
                        $item['price'] = OrderProduct::where(['cate_1'=>$item['id']])->whereYear('create_time')->sum('vip_price');
                        $item['last_price'] = OrderProduct::where(['cate_1'=>$item['id']])->whereYear('create_time', 'last year')->sum('vip_price');
                    }else{
                        $item['price'] = OrderProduct::whereNotIn('cate_1', [1,2,3])->whereYear('create_time')->sum('vip_price');
                        $item['last_price'] = OrderProduct::whereNotIn('cate_1', [1,2,3])->whereYear('create_time', 'last year')->sum('vip_price');
                    }

                    $item['price_add'] = $item['price'] - $item['last_price'];
                    $item['price_rate'] = $item['last_price'] > 0 ? round(100*$item['price_add']/$item['last_price'], 2).'%' : '-';
                }elseif ('Statistics_catePriceMonth' == $name){
                    if(in_array($item['id'],[1,2,3])){
                        $item['price'] = OrderProduct::where(['cate_1'=>$item['id']])->whereMonth('create_time')->sum('vip_price');
                        $item['last_price'] = OrderProduct::where(['cate_1'=>$item['id']])->whereMonth('create_time', 'last year')->sum('vip_price');
                    }else{
                        $item['price'] = OrderProduct::whereNotIn('cate_1', [1,2,3])->whereMonth('create_time')->sum('vip_price');
                        $item['last_price'] = OrderProduct::whereNotIn('cate_1', [1,2,3])->whereMonth('create_time', 'last year')->sum('vip_price');
                    }

                    $item['price_add'] = $item['price'] - $item['last_price'];
                    $item['price_rate'] = $item['last_price'] > 0 ? round(100*$item['price_add']/$item['last_price'], 2).'%' : '-';
                }
            }

            return json(['code' => 0, 'data' => $list, 'count' => 4]);

        } catch (\Exception $e) {
            return json(['code'=>-1, 'message'=>$e->getMessage()]);
        }
    }*/

    #---------------------------------------------------------------分区业绩统计end-----------------------------------------#

    #---------------------------------------------------------------商品单品统计start-----------------------------------------#

    /**
     * 年度消费人数
     * @return string
     * @throws \Exception
     */
    public function productUserYear(): string
    {
        $tableFront = TableFront::getInstance()
            ->addRow(['field' => 'product_id', 'title' => '商品id'])
            ->addRow(['field' => 'product_name', 'title' => '商品名称'])
            ->addRow(['field' => 'user_count', 'title' => '年度消费人数'])
            ->addRow(['field' => 'user_add', 'title' => '年度新增消费人数'])
            ->addRow(['field' => 'user_rate', 'title' => '年度增长率']);

        return TableBuilder::getInstance()
            ->addColumns($tableFront)
            ->setSearch($this->setProductSearch(),false)
            ->fetch('product');
    }

    /**
     * 月度消费人数
     * @return string
     * @throws \Exception
     */
    public function productUserMonth(): string
    {
        $tableFront = TableFront::getInstance()
            ->addRow(['field' => 'product_id', 'title' => '商品id'])
            ->addRow(['field' => 'product_name', 'title' => '商品名称'])
            ->addRow(['field' => 'user_count', 'title' => '月度消费人数'])
            ->addRow(['field' => 'user_add', 'title' => '月度新增消费人数'])
            ->addRow(['field' => 'user_rate', 'title' => '月度增长率']);

        return TableBuilder::getInstance()
            ->addColumns($tableFront)
            ->setSearch($this->setProductSearch(),false)
            ->fetch('product');
    }

    /**
     * 年度业绩
     * @return string
     * @throws \Exception
     */
    public function productPriceYear(): string
    {
        $tableFront = TableFront::getInstance()
            ->addRow(['field' => 'product_id', 'title' => '商品id'])
            ->addRow(['field' => 'product_name', 'title' => '商品名称'])
            ->addRow(['field' => 'consume_sum', 'title' => '年度业绩'])
            ->addRow(['field' => 'consume_add', 'title' => '年度新增业绩'])
            ->addRow(['field' => 'consume_rate', 'title' => '年度增长率']);

        return TableBuilder::getInstance()
            ->addColumns($tableFront)
            ->setSearch($this->setProductSearch(),false)
            ->fetch('product');
    }

    /**
     * 月度业绩
     * @return string
     * @throws \Exception
     */
    public function productPriceMonth(): string
    {
        $tableFront = TableFront::getInstance()
            ->addRow(['field' => 'product_id', 'title' => '商品id'])
            ->addRow(['field' => 'product_name', 'title' => '商品名称'])
            ->addRow(['field' => 'consume_sum', 'title' => '月度业绩'])
            ->addRow(['field' => 'consume_add', 'title' => '月度新增业绩'])
            ->addRow(['field' => 'consume_rate', 'title' => '月度增长率']);

        return TableBuilder::getInstance()
            ->addColumns($tableFront)
            ->setSearch($this->setProductSearch(),false)
            ->fetch('product');
    }

    public function setProductSearch(): TableFront
    {
        $tableFront = TableFront::getInstance();
        $tableFront->setSearch(['product_name', '输入商品ID，名称', 'input', '', '', '', 'like']);
        $tableFront->setSearch(['user_min', '最低消费人数', 'input', '', '', '', 'like']);
        $tableFront->setSearch(['user_max', '最高消费人数', 'input', '', '', '', 'like']);
        $tableFront->setSearch(['user_add_min', '最低新增人数', 'input', '', '', '', 'like']);
        $tableFront->setSearch(['user_add_max', '最高新增人数', 'input', '', '', '', 'like']);

        return $tableFront;
    }

    public function getProductList(): Json
    {
        $param = $this->request->param();
        $name = $param['name'];

        try {
            $page = $param['page'] ?? 1;
            $limit = $param['limit'] ?? 10;

            $listRows = [
                'page' => $page,
                'list_rows' => $limit
            ];

            if('Statistics_productUserYear' == $name || 'Statistics_productPriceYear' == $name){
                $sql = StatisticsProductYear::where('year', date("Y"));
            }else{
                $sql = StatisticsProductMonth::where('month', date("Y-m"));
            }

            if(!empty($param['product_name'])) $sql->whereLike('product_id|product_name', '%'.$param['product_name'].'%');
            if(!empty($param['user_min'])) $sql->where('user_count', '>=', $param['user_min']);
            if(!empty($param['user_max'])) $sql->where('user_count', '<=', $param['user_max']);
            if(!empty($param['user_add_min'])) $sql->where('user_add', '>=', $param['user_add_min']);
            if(!empty($param['user_add_max'])) $sql->where('user_add', '<=', $param['user_add_max']);

            $list = $sql->paginate($listRows);
            $data = $list->items();
            $count = $list->total();

            return json(['code' => 0, 'data' => $data, 'count' => $count]);

        } catch (\Exception $e) {
            return json(['code'=>-1, 'message'=>$e->getMessage()]);
        }
    }

    #---------------------------------------------------------------商品单品统计end-----------------------------------------#

    #---------------------------------------------------------------地区数据统计start---------------------------------------#
    /**
     * 年度消费人数
     * @return string
     * @throws \Exception
     */
    public function areaUserYear(): string
    {
        $tableFront = TableFront::getInstance()
            ->addRow(['field' => 'district_name', 'title' => '地区'])
            ->addRow(['field' => 'user_count', 'title' => '年度消费人数'])
            ->addRow(['field' => 'user_add', 'title' => '年度新增消费人数'])
            ->addRow(['field' => 'user_rate', 'title' => '年度增长率']);

        return TableBuilder::getInstance()
            ->addColumns($tableFront)
            ->setSearch($this->setAreaSearch(),false)
            ->fetch('area');
    }

    /**
     * 月度消费人数
     * @return string
     * @throws \Exception
     */
    public function areaUserMonth(): string
    {
        $tableFront = TableFront::getInstance()
            ->addRow(['field' => 'district_name', 'title' => '地区'])
            ->addRow(['field' => 'user_count', 'title' => '月度消费人数'])
            ->addRow(['field' => 'user_add', 'title' => '月度新增消费人数'])
            ->addRow(['field' => 'user_rate', 'title' => '月度增长率']);

        return TableBuilder::getInstance()
            ->addColumns($tableFront)
            ->setSearch($this->setAreaSearch(),false)
            ->fetch('area');
    }

    /**
     * 年度业绩
     * @return string
     * @throws \Exception
     */
    public function areaPriceYear(): string
    {
        $tableFront = TableFront::getInstance()
            ->addRow(['field' => 'district_name', 'title' => '地区'])
            ->addRow(['field' => 'consume_sum', 'title' => '年度业绩'])
            ->addRow(['field' => 'consume_add', 'title' => '年度新增业绩'])
            ->addRow(['field' => 'consume_rate', 'title' => '年度增长率']);

        return TableBuilder::getInstance()
            ->addColumns($tableFront)
            ->setSearch($this->setAreaSearch(),false)
            ->fetch('area');
    }

    /**
     * 月度业绩
     * @return string
     * @throws \Exception
     */
    public function areaPriceMonth(): string
    {
        $tableFront = TableFront::getInstance()
            ->addRow(['field' => 'district_name', 'title' => '地区'])
            ->addRow(['field' => 'consume_sum', 'title' => '月度业绩'])
            ->addRow(['field' => 'consume_add', 'title' => '月度新增业绩'])
            ->addRow(['field' => 'consume_rate', 'title' => '月度增长率']);

        return TableBuilder::getInstance()
            ->addColumns($tableFront)
            ->setSearch($this->setAreaSearch(),false)
            ->fetch('area');
    }

    public function setAreaSearch()
    {
        $tableFront = TableFront::getInstance();
        $tableFront->setSearch(['district_name', '输入地区名称', 'input', '', '', '', 'like']);
        $tableFront->setSearch(['user_min', '最低消费人数', 'input', '', '', '', 'like']);
        $tableFront->setSearch(['user_max', '最高消费人数', 'input', '', '', '', 'like']);
        $tableFront->setSearch(['user_add_min', '最低新增人数', 'input', '', '', '', 'like']);
        $tableFront->setSearch(['user_add_max', '最高新增人数', 'input', '', '', '', 'like']);

        return $tableFront;
    }

    public function getAreaList(): Json
    {
        $param = $this->request->param();
        $name = $param['name'];

        try {
            $page = $param['page'] ?? 1;
            $limit = $param['limit'] ?? 10;

            $listRows = [
                'page' => $page,
                'list_rows' => $limit
            ];

            if('Statistics_areaUserYear' == $name || 'Statistics_areaPriceYear' == $name){
                $sql = StatisticsAreaYear::where('year', date("Y"));
            }else{
                $sql = StatisticsAreaMonth::where('month', date("Y-m"));
            }

            if(!empty($param['district_name'])) $sql->whereLike('district_name', '%'.$param['district_name'].'%');
            if(!empty($param['user_min'])) $sql->where('user_count', '>=', $param['user_min']);
            if(!empty($param['user_max'])) $sql->where('user_count', '<=', $param['user_max']);
            if(!empty($param['user_add_min'])) $sql->where('user_add', '>=', $param['user_add_min']);
            if(!empty($param['user_add_max'])) $sql->where('user_add', '<=', $param['user_add_max']);

            $list = $sql->paginate($listRows);
            $data = $list->items();
            $count = $list->total();

            return json(['code' => 0, 'data' => $data, 'count' => $count]);

        } catch (\Exception $e) {
            return json(['code'=>-1, 'message'=>$e->getMessage()]);
        }
    }
    #---------------------------------------------------------------地区数据统计end-----------------------------------------#


    #------默认不展示数据，根据时间段搜索后，统计并显示时间段内消费过的盟主、市代、区代的编号、昵称、用户名、手机号、消费金额  start----#
    /**
     *
     */
    public function userConsumed(): string
    {
        $tableFront = TableFront::getInstance()
            ->addRow(['field' => 'code', 'title' => '会员编号'])
            ->addRow(['field' => 'nickname', 'title' => '昵称'])
            ->addRow(['field' => 'real_name', 'title' => '姓名'])
            ->addRow(['field' => 'mobile', 'title' => '手机号'])
            ->addRow(['field' => 'level', 'title' => '级别'])
            ->addRow(['field' => 'consume_amount', 'title' => '消费金额（元）']);

        return TableBuilder::getInstance()
            ->addColumns($tableFront)
            ->setSearch($this->setSearch())
            ->fetch('user_consumed');
    }

    public function getUserConsumed(): Json
    {
        try{
            $param = $this->request->param();
            $listRows = [
                'page' => $param['page'] ?? 1,
                'list_rows' => $param['limit'] ?? 10
            ];

            $list = [];
            $count = 0;

            if(!empty($param['times'])){
                list($startDate, $endDate) = explode(' - ', $param['times']);
                $startTime = strtotime($startDate);
                $endTime = strtotime($endDate.' 23:59:59');
                $result = Order::where('pay_state',2)->whereBetweenTime('pay_time',$startTime, $endTime)->group('uid')->column("sum(real_amount) as real_amount","uid");

                $uids = array_keys($result);

                $user = User::whereIn('id', $uids)->where(function ($query){
                    $query->where('level_id',5)->whereOr('agent_level','>',0);
                })->field('id,code,nickname,real_name,mobile,level_id,agent_level')->paginate($listRows)->each(function ($item) use ($result){
                    $level = [];
                    if($item->level_id == 5) array_push($level,'盟主');
                    if($item->agent_level == 1){
                        array_push($level,'区代');
                    }elseif($item->agent_level == 2){
                        array_push($level,'市代');
                    }elseif($item->agent_level == 3){
                        array_push($level,'市代兼区代');
                    }
                    $item->level = implode('，',$level);
                    $item->consume_amount = isset($result[$item->id]) ? $result[$item->id] : 0;
                })->toArray();

                $list = $user['data'];
                $count = $user['total'];
            }

            return json(['code' => 0, 'data' => $list, 'count' => $count]);
        } catch (\Exception $e) {
            return json(['code'=>-1, 'message'=>$e->getMessage()]);
        }
    }
    #------默认不展示数据，根据时间段搜索后，统计并显示时间段内消费过的盟主、市代、区代的编号、昵称、用户名、手机号、消费金额  end------#
}